import 'package:flutter/material.dart' hide Drawer;
import './pages/cate/index.dart';
import './pages/dynamic/index.dart';
import './pages/index/index.dart';
import 'package:hairen_app/server/state/UserInfoModel.dart';
import 'package:hairen_app/server/saveInfo.dart';
import 'package:hairen_app/server/server.dart';
import 'dart:async';
import 'package:flutter_web_browser/flutter_web_browser.dart';
void main() {
  runApp(new MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
//    UserInfoModel.isLogin=true;
    return new MaterialApp(
      title: '嗨人电影',
      theme: new ThemeData(
        buttonColor: Color.fromRGBO(251, 114, 153, 1.0),
        primaryColor:Color.fromRGBO(251, 114, 153, 1.0),
        backgroundColor:Color.fromRGBO(251, 114, 153, 1.0),
        iconTheme: IconThemeData(
            color:  Color.fromRGBO(251, 114, 153, 1.0)
        ),

        bottomAppBarColor: new Color.fromRGBO(244, 245, 245, 1.0),    //设置底部导航的背景色
//        scaffoldBackgroundColor: new Color.fromRGBO(244, 245, 245, 1.0),    //设置页面背景颜色
        primaryIconTheme: new IconThemeData(color: Color.fromRGBO(255, 255, 255, 1.0),), //按钮衍射
        indicatorColor: Color.fromRGBO(251, 114, 153, 1.0),
        primaryTextTheme: new TextTheme(    //设置文本样式
            title: new TextStyle(color:Color.fromRGBO(255, 255, 255, 1.0), fontSize: 16.0)),
      ),
      home: new MyHomePage(title: '(｡･∀･)ﾉﾞ嗨人视频'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  int currentIndex=0;
  final String title;
  MyHomePage({Key key, this.title,this.currentIndex=0}) : super(key: key);
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {

 final List<Widget> tabBox=[
   new IndexPage(),
   new CatePage(),
   new DynamicPage(),
 ];

 int currentIndex=0;
 Widget currentPage;

 Map userInfo = {};
 String token = "";
 getInfo() {
   SaveInfo.getUserToken().then((Map ret) {
     setState(() {
       userInfo = ret['userInfo'];
       token = ret['token'];
       if(token!=''){
         UserInfoModel.isLogin=true;
       }else{
         UserInfoModel.isLogin=false;
       }
     });
   });
 }

 @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.currentIndex=widget.currentIndex;
    this.currentPage=tabBox[this.currentIndex];
    getInfo();
    updateVersion();

  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

  }

  //版本检测
  updateVersion() async {
    Map data=await  Server.getVersion();
    print(data);
    if(data['code']==1){
      if(double.parse(data['data']['version'])>UserInfoModel.version){
        alertVersion(data['data']['file']);
      }
    }
  }

 Future alertVersion(String path) async{
   return   showDialog<Null>(
     context: context,
     barrierDismissible: false, // user must tap button!
     builder: (BuildContext context) {
       return new AlertDialog(
         title: new Text("发现新版本,是否更新"),
         actions: <Widget>[
           new Container(
             child:  new RaisedButton(
               child: new Text('确认',style: new TextStyle( color: Colors.white,),),
               onPressed: () {
                 FlutterWebBrowser.openWebPage(url: path, androidToolbarColor: Colors.deepPurple);
                 Navigator.of(context).pop();
               },
             ),
             alignment: Alignment.centerLeft,
           ),
           new RaisedButton(
             child: new Text('取消',style: new TextStyle( color: Colors.white,),),
             color: Colors.black12.withOpacity(0.5),
             onPressed: () {
               Navigator.of(context).pop();
             },
           ),
         ],
       );
     },
   );
 }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
//      body: this.currentPage,
      body: this.currentPage,

      bottomNavigationBar:new BottomNavigationBar(
        items: [
          new BottomNavigationBarItem(icon: new Icon(Icons.home), title: new Text("首页",style: new TextStyle(fontSize: 13.0),)),
          new BottomNavigationBarItem(icon: new Icon(Icons.category), title: new Text("分类",style: new TextStyle(fontSize: 13.0))),
          new BottomNavigationBarItem(icon: new Icon(Icons.filter_drama), title: new Text("动态",style: new TextStyle(fontSize: 13.0))),
        ],
        fixedColor:Color.fromRGBO(251, 114, 153, 1.0),
        iconSize:18.0,
        currentIndex: this.currentIndex ,
        type: BottomNavigationBarType.fixed,
        onTap: (index){
          setState(() {
            this.currentIndex=index;
            this.currentPage=tabBox[this.currentIndex];
          });
        },
      ),
    );
  }


}
